package com.kgo.eidtor.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.kgo.eidtor.dao.UserBeanMapper;
import com.kgo.eidtor.entity.UserBean;
import com.kgo.eidtor.service.UserService;
import com.kgo.eidtor.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author keepgoon
 * @Description: TODO
 * @date 2019/2/1410:50
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserBeanMapper userBeanMapper;

    @Override
    public String login(UserBean user) {
        if (StringUtils.isBlank(user.getUserName())){
            return ResultUtil.query(-1,null,"用户名不能为空！");
        }
        if (StringUtils.isBlank(user.getPassword())){
            return ResultUtil.query(-1,null,"登录密码不能为空！");
        }
        List<UserBean> userInfo = userBeanMapper.getloginInfo(user);
        if (userInfo.size()<=0){
            return ResultUtil.query(-1,null,"登录失败，用户名或密码错误！");
        }
        if (userInfo.get(0).getUserStatus().equals(UserBean.Status.NewRegister)){
            return ResultUtil.query(-1,null,"登录失败，您的注册信息尚未审核通过，请联系管理员！");
        }
        return ResultUtil.query(1,userInfo.get(0),"登录成功");
    }

    @Override
    public String getUserInfoById(String id) {
        if (StringUtils.isBlank(id)){
            return ResultUtil.query(-1,null,"查询失败。用户编号不能为空！");
        }
        UserBean thisUser = userBeanMapper.getUserInfoById(id);
        if (ObjectUtil.isNull(thisUser)){
            return ResultUtil.query(-1,null,"查询失败。无指定用户数据！");
        }else{
            return ResultUtil.query(1,thisUser,"查询用户信息成功");
        }
    }

    @Override
    public String addUser(UserBean user) {
        if(StringUtils.isNotBlank(user.getUserName())){
            UserBean testRepetition =  new UserBean();
            testRepetition.setUserName(user.getUserName());
            int count = userBeanMapper.getUserCount(testRepetition);
            if(count>0){
                return ResultUtil.add(0,"新增用户信息失败！当前用户名已经存在，请重新输入！");
            }
        }else{
            return ResultUtil.add(0,"新增用户信息失败！用户名不能为空和空格");
        }
        if(StringUtils.isBlank(user.getPassword())){
            return ResultUtil.add(0,"新增用户信息失败！密码不能为空和空格");
        }
        if (StringUtils.isBlank(user.getId())){
            user.setId(IdUtil.simpleUUID());
        }
        int count = userBeanMapper.insert(user);
        if (count<=0){
            return ResultUtil.add(0,"新增用户信息失败！");
        }
        return ResultUtil.add(count,"新增用户信息成功！");
    }

    @Override
    public String getUsers(UserBean user) {
        int count = userBeanMapper.getUserCount(user);
        if (count<=0){
            return ResultUtil.query(0,null,"用户信息查询失败，无符合条件用户信息！");
        }

        return ResultUtil.query(count,userBeanMapper.getUsers(user),"用户信息查询成功！");
    }
}
